#from django.conf import settings
from lbforum.models import TitleConfig,FriendLink,create_TitleConfig,key_TitleConfig
from google.appengine.api import memcache
from django.contrib.auth import REDIRECT_FIELD_NAME

import debt

def friend_link_config(request):
    key="friend_link"
    lst_link=memcache.get(key)
    if lst_link is not None:
        return {"friend_link":lst_link}
    
    #not found in cache
    q_friend_link=FriendLink.all()
    #10 at most
    lst_link=q_friend_link.fetch(10)
    if lst_link is None or len(lst_link) == 0:
        fl=FriendLink()
        fl.put()
        lst_link=[fl]
        
    memcache.add(key,lst_link,60)
    return {"friend_link":lst_link}
    
def ctx_config(request):
    
    ctx=debt.get_obj(TitleConfig, key_TitleConfig,on_get_None=create_TitleConfig)
    """
    ctx=memcache.get(key)
    if ctx is not None:
        return ctx.to_cfg()
    #not found in cache
    ctx=TitleConfig.get_by_key_name(key)
    if ctx is not None:
        memcache.add(key,ctx,30)
        return ctx.to_cfg()
    
    #not found ,has to create it
    """
    
    return ctx.to_cfg()

    #return settings.CTX_CONFIG

def refer_config(request):
    #url_refer = request.META.get('HTTP_REFERER',"/")
    return {"url_current": request.META['PATH_INFO']}
